<?php
namespace app\admin\controller;
use app\admin\model\Member as MemberModel;
use app\admin\model\User;
use think\Request;
use think\Db;
class Member extends BaseController
{
    public function __construct()
    {
        $this->member = new MemberModel();
        $this->user = new user();
        parent::__construct();
    }
    //会员列表
    public function index()
    {
        $list = $this->member->getMemberList();
        $page = $list->render();
        $num = count($list);

        $new_list = [];
        foreach($list as $k=>$v)
        {
            $new_list[$k] = $v;
            $p_user = Db::name('users')->where('user_id',$v['pid'])->find();
            $new_list[$k]['p_user'] = $p_user['user_name'];
        }

        
        $this->assign("page",$page);
        $this->assign("list",$new_list);
        $this->assign("count",$num);
        $this->assign('username_val',input('username'));
        return $this->fetch();
    }
    //会员信息
    public function member_show(){
        $uid = input("get.uid");
        $res = $this->member->getMember($uid);
        $this->assign("res",$res);
        return $this->fetch();
    }
    //修改会员状态
    public function member_check(){
        $id = input("post.id");
        $res = $this->member->memberCheck($id);
        return $res;
    }
    //修改用户
    public function member_add(){
        if($this->request->post()){
            $data = input("post.");
            $res = $this->member->memberEdit($data);
            if($res){
                $this->success("修改成功");
            }else{
                $this->error("修改失败");
            }
        }else{
            $uid = input("get.uid");
            $res = $this->member->getMember($uid);
            $p_user = Db::name('users')->where('user_id',$res['pid'])->find();
            if($p_user == "")
            {
                $p_user['user_name'] = "";
            }
            $res1 = $this->member->getBankName();
            $recommend = $this->member->getrecommend($res['pid']);
            $this->assign('result',$res1);
            $this->assign("data",$res);
            $this->assign('p_user',$p_user['user_name']);
            $this->assign("recom",$recommend);
            return $this->fetch();
        }
    }

    // 充值筹码
    public function recharge(){
            $uid = input("get.uid");
            $res = $this->member->getMember($uid);
            $this->assign('result',$res);
            return $this->fetch();
    }

    public function add(){
        $request = Request::instance();
        if($request->isAjax()){

            $data = $request->post();
            $res = $this->member->add($data);
            return $res;
        }else{
            $res = $this->member->getBankName();
            $this->assign('result',$res);
            return $this->fetch();
        }
    }
	public function edit(){
		$request = Request::instance();
        $data = $request->post();
		return $this->member->memberEdit($data);
		
    }
    //
    public function edit_recharge(){
        $request = Request::instance();
		$data = $request->post();
		return $this->member->rechargeEdit($data);
    }
    //删除用户
    public function member_del(){
        $id = input("post.id");
        $res = $this->member->memberDel($id);
        return $res;
    }
    //修改管理员密码
    public function check_pwd(){
        if($this->request->post()){
            $data = input("post.");
            $res = $this->user->checkPwd($data);
            return $res;
        }else{
            return $this->fetch();
        }
    }
    //重置二级密码
    public function member_checkpaypwd(){
        $id = input("post.id");
        $res = $this->member->memberCheckPayPwd($id);
        return $res;
    }
    //解冻积分
    public function member_thaw(){
        $id = input("post.id");
        $res = $this->member->memberThaw($id);
        return $res;
    }
    //积分调整
    public function member_recharge(){
        //$res = $this->member->memberRecharge();
        if(input("post.")){
            return $this->member->memberRecharge();
        }else{
            $uid = input('uid');
            $this->assign("uid",$uid);
            return $this->fetch();
        }
    }
}
